#!/bin/sh

# @file bono.monit
#
# Copyright (C) Metaswitch Networks 2015
# If license terms are provided to you in a COPYING file in the root directory
# of the source code repository by which you are accessing this code, then
# the license outlined in that COPYING file applies to your use.
# Otherwise no rights are granted except for those provided to you by
# Metaswitch Networks in a separate written agreement.

# Set up the monit configuration for bono with the right IP addresses and ports
cat > /etc/monit/conf.d/bono.monit <<EOF
# Check the Bono process.

# Monitor the service's PID file and memory use.
check process bono_process with pidfile /var/run/bono/bono.pid
  group bono

  start program = "/etc/init.d/bono start"
  stop program = "/etc/init.d/bono stop"
  restart program = "/etc/init.d/bono restart"

  # Check the service's resource usage, and abort the process if it's too high. This will
  # generate a core file and trigger diagnostics collection. 
  if memory > 80% then exec "/etc/init.d/bono abort"

# Check the SIP interface. This depends on the Bono process (and so won't run
# unless the Bono process is running)
check program poll_bono with path "/usr/share/clearwater/bin/poll_bono.sh"
  group bono
  depends on bono_process

  # Aborting generates a core file and triggers diagnostic collection.
  if status != 0 for 2 cycles then exec "/etc/init.d/bono abort"
EOF
chmod 0644 /etc/monit/conf.d/bono.monit

# Force monit to reload its configuration
reload clearwater-monit || true

